package pers.hl.module_main.common

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.fragment.app.FragmentActivity
import pers.hl.lib_global.common.Const
import pers.hl.lib_global.model.bean.Book
import pers.hl.module_main.CommonEditActivity
import pers.hl.module_main.book_detail.BookDetailActivity
import pers.hl.module_main.mine.UserHomeActivity
import pers.hl.module_main.search.SearchActivity

object PageHelper {

    fun navToSearch(context: Context) {
//        val intent = Intent(context, SearchActivity::class.java)
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//        KTApplication.getInstance().startActivity(intent)
        startActivity(context, SearchActivity::class.java)
    }

    fun startActivity(context: Context, clazz: Class<*>) {
        val intent = Intent(context, clazz)
        context.startActivity(intent)
    }

    /**
     * 只适用于activity
     */
    fun startActivityForResult(context: FragmentActivity?, clazz: Class<*>, requestCode: Int) {
        val intent = Intent(context, clazz)
        context?.startActivityForResult(intent, requestCode)
    }

    fun startActivity(context: Context, clazz: Class<*>, extra: Bundle) {
        val intent = Intent(context, clazz)
        intent.putExtra(Const.KEY.KEY_DATA_COMMON_BUNDLE, extra)
        context.startActivity(intent)
    }

    fun <T: Parcelable> navToCommonEditPage(context: FragmentActivity, data: T?, requestCode: Int, type: Int) {
        val intent = Intent(context, CommonEditActivity::class.java)
        intent.putExtra(CommonEditActivity.KEY_EDIT_TYPE, type)
        val bundle = Bundle()
        data?.let {
            bundle.putParcelable(Const.KEY.KEY_DATA_COMMON_EDIT, it)
        }
        intent.putExtra(Const.KEY.KEY_DATA_COMMON_EDIT, bundle)
        context.startActivityForResult(intent, requestCode)
    }

    fun startBookDetailPage(context: Context, data: Book) {
        val intent = Intent(context, BookDetailActivity::class.java)
        intent.putExtra(BookDetailActivity.KEY_DATA_BOOK, data)
        context.startActivity(intent)
    }

    fun startUserHomePage(context: Context, userId: Int? = null, canEdit: Boolean = false) {
        val intent = Intent(context, UserHomeActivity::class.java)
        userId?.let { intent.putExtra(UserHomeActivity.KEY_DATA_USER_ID, it) }
        intent.putExtra(UserHomeActivity.KEY_CAN_EDIT, canEdit)
        context.startActivity(intent)
    }


}